﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using System.Text;
using System.Data.Common;

using log4net;

namespace DataDesign.MPP.Domain
{
    public class BaseDomain<T> where T:class, ICommonFunctions<T>
    {
        readonly static ILog logger = LogManager.GetLogger(typeof(T));
        #region Common Methods
        public static Table<T> GetTable()
        {
            DBContext db = new DBContext();
            return db.GetTable<T>();
        }

        public static Table<T> GetTable(DBContext db)
        {
            return db.GetTable<T>();
        }
        public static List<T> GetAll()
        {
            var list = from i in GetTable() select i;
            logger.Debug("list.count: " + list.Count());
            return list.ToList();
        }        
        #endregion

        #region Insert,Delete,Update
        public void Insert()
        {
          
            DBContext db = new DBContext();
            db.Insert<T>(this as T);
        }

        public void Delete()
        {
            DBContext db = new DBContext();
            db.Delete<T>(this as T);
        }

        public void Update()
        {
          
            DBContext db = new DBContext();
            db.Update<T>(this as T);
        }


        #endregion
    }

    public interface ICommonFunctions<T>
    {
        T GetByPrimaryKey();
    }

}
